<script setup name="Project">
import { getProjectList, delProject } from '@/api/subject/subject'
import NewProjectDlg from './components/NewProjectDlg'
const { proxy } = getCurrentInstance()
const route = useRoute()
const router = useRouter()
const showSearch = ref(true)
const loading = ref(true)
const projectList = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const selectName = ref('')
const dialogVisible = ref(false)
const total = ref(0)
const dlgTitle = ref('')

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  projectName: undefined,
  projectCode: undefined,
  subjectId: route.params.subjectId
})
const projectInfo = ref({})
const getList = () => {
  loading.value = true
  getProjectList(queryParams.value).then(response => {
    projectList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}
function resetQuery () {
  proxy.resetForm('queryRef')
  queryParams.value.pageNum = 1
}
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  multiple.value = !selection.length
  single.value = selection.length !== 1
  selectName.value = selection.map(item => item.projectName)
}
const handleAdd = () => {
  dlgTitle.value = '新增任务'
  dialogVisible.value = true
  projectInfo.value = {
    id: '',
    projectName: '',
    projectCode: '',
    remark: ''
  }
}
const handleDelete = (row) => {
  selectName.value = row.projectName || selectName.value
  const infoIds = row.id || ids.value
  console.log(infoIds)
  proxy.$modal.confirm('是否确认删除任务名称为"' + selectName.value + '"的数据项?').then(function () {
    return delProject(infoIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).catch(() => {})
}
const addSuccess = () => {
  dialogVisible.value = false
  getList()
}
const handleUpdate = (row) => {
  projectInfo.value = row
  dlgTitle.value = '修改任务'
  dialogVisible.value = true
}
const viewProject = (row) => {
  router.push({ path: '/project/file-data/index/' + row.id })
}
const handleClose = () => {
  const obj = { path: '/subject' }
  proxy.$tab.closeOpenPage(obj)
}
onMounted(() => {
  getList()
})
</script>

<template>
  <div class="app-container">
    <el-divider content-position="left">
      <span
        style="font-size: 16px;color: #409EFF;font-weight: bold"
      >{{ route.params.subjectName }}</span>
    </el-divider>

    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="任务名称"
        prop="projectName"
      >
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入任务名称"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="任务编号"
        prop="projectCode"
      >
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入任务编号"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
        >
          搜索
        </el-button>
        <el-button
          icon="Refresh"
          @click="resetQuery"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>
    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['task:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:delete']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Close"
          @click="handleClose"
        >
          关闭
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @query-table="getList"
      />
    </el-row>
    <el-table
      ref="logininforRef"
      v-loading="loading"
      :data="projectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="任务名称"
        align="center"
        prop="projectName"
      />
      <el-table-column
        label="任务编号"
        align="center"
        prop="projectCode"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        prop="opt"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['task:edit']"
          >
            修改
          </el-button>
          <el-button
            link
            type="warning"
            icon="Files"
            @click="viewProject(scope.row)"
            v-hasPermi="['task:file']"
          >
            文件管理
          </el-button>
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['task:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <NewProjectDlg
      :title="dlgTitle"
      v-model:dialogFormVisible="dialogVisible"
      :project-info="projectInfo"
      @on-success="addSuccess"
    />
  </div>
</template>

<style scoped lang="scss">

</style>
